package com.atguigu.spring6;

import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestUser {

    Logger logger = LoggerFactory.getLogger(TestUser.class);

    @Test
    public void testUserObje() {
        // 1、加载spring配置文件
        ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
        // 2、获取配置创建的对象
        User user = (User) context.getBean("user");
        // 3、使用对象
        user.add();
        // 4、打印对象
        logger.info("user对象：{}", user);
    }

    /**
     * 反射创建对象
     */
    @Test
    public void testUserReflect() throws Exception {
        // 1、获取class对象
        Class clazz = Class.forName("com.atguigu.spring6.User");
        // 2、通过class对象创建对象
//        User user = (User) clazz.newInstance();
        User user = (User) clazz.getDeclaredConstructor().newInstance();
        // 3、使用对象
        user.add();
    }
}
